package org.teststack.Api;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;

import java.util.HashMap;
import java.util.Map;

import static org.teststack.Api.CookieManager.cookies;

public class RestClient {


    // 封装了添加cookies的请求方法
    public static RequestSpecification givenWithCookies() {

        return RestAssured.given()
                .cookies(cookies);
    }

    public static void main(String[] args) {
        loginAndUpdateCookies("liutao", "xQ5Vea/L3CMRNEwoBESSZGSVCnValY4kZcAYe3153B9/dXpazZRG8VLEuMhJdW1RhdZ/cqdUq/5FUdAxeKzdWOmZEj0sVlobwrqzRMgrIPsvyp03QTktVYRUAgloA924e5wLLSx8ECJ4WpUAB8zUa41GkyURFmyVtwxQ3EkLuabQ7V24L8EdzRfBHnFd8zRRd4RkncACXO88rrb4U7uR10ANvR0K4N2Kiai901pyztSuMfXP0fVRBW1lKj+kIONtFufTZoAL48++Q6VVr3r5B9tIC4kkwv0WzQxeOubbVKTNUi1JGnS2CEPPqjuXWJnUAO3u5PRxXmtH5+JN8yXygw==");
    }
    // 示例：登录并更新cookies
    public static void loginAndUpdateCookies(String loginName,String psd) {
        // 假设这里执行登录请求


                Map<String, String> formParams = new HashMap<>();

                formParams.put("loginName", loginName);
                formParams.put("psd", psd);

                Response response = RestAssured.given()
                        .baseUri("http://10.1.10.141:9031")
                        .when()
                        .formParams(formParams)
                        .post("/login.do?method=login_qyzh");
        Map<String, String> cookies = response.getCookies();
        int statusCode = response.getStatusCode();
        String zyOpUser = cookies.get("zyOpUser");

//        System.out.println("Status Code: " + statusCode);
//        System.out.println("zyOpUser= " + zyOpUser);

        // 假设从登录响应中提取了新的cookie
        Map<String, String> newCookies = new HashMap<>();
        // 假设这里填充了newCookies
        newCookies.put("zyOpUser", zyOpUser);
        // 更新全局cookies
        CookieManager.updateCookiesFromLoginResponse(newCookies);

    }
}


